package com.light.mytext.tencentvideo

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.light.mytext.R
import com.tencent.liteav.trtccalling.model.TRTCCalling
import com.tencent.liteav.trtccalling.ui.videocall.TRTCVideoCallActivity
import kotlinx.android.synthetic.main.activity_tencent_login.*
import java.util.*

class TencentLoginActivity : AppCompatActivity() {
    var mType = TRTCCalling.TYPE_VIDEO_CALL
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tencent_login)
        init_Listener()
        initPermission()
    }
    override fun onResume() {
        super.onResume()
        CallService.start(this)
    }
    fun init_Listener() {
        bt_login.setOnClickListener {
            if (bt_login.text == "登录") {
                val userId: String = ed_userid.text.toString().trim { it <= ' ' }
                ProfileManager.getInstance()
                    .login(userId, "", object : ProfileManager.ActionCallback {
                        override fun onSuccess() {
                            bt_login.text = "退出登录"
                            bt_applayvideo.visibility = View.VISIBLE
                        }

                        override fun onFailed(code: Int, msg: String?) {}
                    })
            } else {//退出登录

            }
        }
        bt_applayvideo.setOnClickListener {
            //发起视频
            startCallSomeone()
        }
    }

    /**
     * 开始呼叫某人
     */
    private fun startCallSomeone() {
        if (mType == TRTCCalling.TYPE_VIDEO_CALL) {
            val selfInfo: TRTCVideoCallActivity.UserInfo =
                TRTCVideoCallActivity.UserInfo()
            selfInfo.userId = "111"
            selfInfo.userAvatar =
                "https://imgcache.qq.com/qcloud/public/static//avatar9_100.20191230.png"
            selfInfo.userName = "111"
            val callUserInfoList: MutableList<TRTCVideoCallActivity.UserInfo> =
                ArrayList<TRTCVideoCallActivity.UserInfo>()
            val callUserInfo: TRTCVideoCallActivity.UserInfo =
                TRTCVideoCallActivity.UserInfo()
            callUserInfo.userId = "222"
            callUserInfo.userAvatar =
                "https://imgcache.qq.com/qcloud/public/static//avatar0_100.20191230.png"
            callUserInfo.userName = "222"
            callUserInfoList.add(callUserInfo)
            ToastUtils.showShort("视频呼叫:" + callUserInfo.userName)
            TRTCVideoCallActivity.startCallSomeone(this, selfInfo, callUserInfoList)
        }
    }

    private fun initPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PermissionUtils.permission(
                PermissionConstants.STORAGE,
                PermissionConstants.MICROPHONE,
                PermissionConstants.CAMERA
            )
                .request()
        }
    }
}
